home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / rw / DComplexVec.h < prev    next >
C/C++ Source or Header  |  1989-08-18  |  8KB  |  235 lines

  1. #ifndef DCOMPLEXVEC_H
  2. #define DCOMPLEXVEC_H
  3. #pragma once
  4.  
  5. /*
  6.  *    Declarations for DComplex Precision Vectors
  7.  *
  8.  *    Copyright (C) 1988, 1989.
  9.  *
  10.  *    Dr. Thomas Keffer
  11.  *    Rogue Wave Associates
  12.  *    P.O. Box 85341
  13.  *    Seattle WA 98145-1341
  14.  *
  15.  *    Permission to use, copy, modify, and distribute this
  16.  *    software and its documentation for any purpose and
  17.  *    without fee is hereby granted, provided that the
  18.  *    above copyright notice appear in all copies and that
  19.  *    both that copyright notice and this permission notice
  20.  *    appear in supporting documentation.
  21.  *    
  22.  *    This software is provided "as is" without any
  23.  *    expressed or implied warranty.
  24.  *
  25.  *
  26.  *    @(#)DComplexVec.h    2.1    8/18/89
  27.  */
  28.  
  29. /*    This code is designed to be as compatible as possible with the
  30.  *    NIH Vector classes, while preserving efficiency.  These Vectors
  31.  *    are NOT based on the NIH "Object" class, making them much
  32.  *    smaller.  They also implement reference counting, making them
  33.  *    faster. 
  34.  */
  35.  
  36.  
  37. #ifdef NO_VECTOR_MATHFUN
  38. #include "vdefs.h"
  39. #include <math.h>
  40. #else
  41. #include "DoubleVec.h"
  42. #endif
  43. #include "RComplex.h"
  44.  
  45. class istream;
  46. class ostream;
  47. class DComplexVec;
  48. class DoubleVec;
  49.  
  50. class DComplexBlock {
  51.   unsigned short      refs;        // Number of references
  52.   unsigned          npts;        // Number of elements
  53.   DComplex            array[1];    // The data
  54.   friend        DComplexVec;
  55. public:
  56.   DComplexBlock(unsigned n);
  57.   DComplexBlock(unsigned n, DComplex val);
  58.   DComplexBlock(unsigned n, DComplex val, DComplex by);
  59.   ~DComplexBlock();
  60.  
  61.   void            add_reference()    {refs++;}
  62.   unsigned        references()    {return refs;}
  63.   DComplex*        data()        {return array;}
  64. };
  65.  
  66. class DComplexVec {
  67.   DComplexBlock*    block;
  68.   DComplex*        begin;
  69.   unsigned        npts;
  70.   int            step;
  71.  
  72.   static int        numberPerLine; // For printing
  73.   DComplexVec(DComplexVec&, int, unsigned, int); // For slices
  74. protected:
  75.   void            boundsErr(int);
  76.   void            boundsCheck(int);
  77.   void            lengthErr(int);
  78.   void            lengthCheck(int i)    {if(npts!=i) lengthErr(i);}
  79.   void            emptyErr(const char* fname);
  80.   void            sliceErr(unsigned, int, unsigned, int);
  81. public:
  82.   DComplexVec();
  83.   DComplexVec(unsigned n);
  84.   DComplexVec(unsigned n, DComplex val);
  85.   DComplexVec(unsigned n, DComplex val, DComplex by);
  86.   DComplexVec(const DComplexVec& a);
  87.   DComplexVec(const DoubleVec& re); // Conversion
  88.   DComplexVec(const DoubleVec& re, const DoubleVec& im);
  89.   DComplexVec(const DComplex* dat, unsigned n);  // Copy of dat will be made
  90.   ~DComplexVec();
  91.   
  92.   DComplexVec        slice(int start, unsigned lgt, int strider=1);
  93.   
  94.   DComplex*        data()        {return begin;}
  95.   unsigned        length()    {return npts;}
  96.   int            stride()    {return step;}
  97.  
  98.   DComplexVec&        reference(DComplexVec& v);    // Reference self to v
  99.   DComplexVec        deepCopy();    // copy of self with distinct instance variables 
  100.   DComplexVec        copy()        {return deepCopy();} // Synonym for deepCopy()
  101.   void            deepenShallowCopy();    // Insures only 1 reference to data
  102.   void            resize(unsigned);    // Will pad with zeroes if necessary
  103.  
  104.   void            scanFrom(istream& s); // Read to eof
  105.   void            printOn(ostream& s);  // Pretty print
  106.   void            setFormatting(int);   // Change # items per line
  107.   
  108.   // Indexing:
  109.   DComplex&        operator[](int i);    // With bounds checking
  110.   DComplex&        operator()(int i);    // With optional bounds checking
  111.   
  112.   // Assignment:
  113.   DComplexVec&        operator=(const DComplexVec& v); // Must be same length as v
  114.   DComplexVec&        operator=(DComplex);
  115.   
  116.   // Arithmetic operators:
  117. //DComplexVec&        operator++();
  118. //DComplexVec&        operator--();
  119.   DComplexVec&        operator+=(const DComplexVec&);
  120.   DComplexVec&        operator+=(DComplex);
  121.   DComplexVec&        operator-=(const DComplexVec&);
  122.   DComplexVec&        operator-=(DComplex);
  123.   DComplexVec&        operator*=(const DComplexVec&);
  124.   DComplexVec&        operator*=(DComplex);
  125.   DComplexVec&        operator/=(const DComplexVec&);
  126.   DComplexVec&        operator/=(DComplex);
  127.   
  128.   // Friendly arithmetic operators:
  129.   friend DComplexVec    operator-(const DComplexVec&);
  130.   friend DComplexVec    operator+(const DComplexVec&);
  131.   friend DComplexVec    operator*(const DComplexVec&,const DComplexVec&);
  132.   friend DComplexVec    operator/(const DComplexVec&,const DComplexVec&);
  133.   friend DComplexVec    operator+(const DComplexVec&,const DComplexVec&);
  134.   friend DComplexVec    operator-(const DComplexVec&,const DComplexVec&);
  135.   friend DComplexVec    operator*(const DComplexVec&,DComplex);
  136.   friend DComplexVec    operator*(DComplex,const DComplexVec&);
  137.   friend DComplexVec    operator/(const DComplexVec&,DComplex);
  138.   friend DComplexVec    operator/(DComplex,const DComplexVec&);
  139.   friend DComplexVec    operator+(const DComplexVec&,DComplex);
  140.   friend DComplexVec    operator+(DComplex,const DComplexVec&);
  141.   friend DComplexVec    operator-(const DComplexVec&,DComplex);
  142.   friend DComplexVec    operator-(DComplex,const DComplexVec&);
  143.   
  144.   
  145. #ifndef NO_VECTOR_MATHFUN
  146.   // Math functions (not all implemented by complex):
  147.   DComplexVec    apply(CmathFunTy);
  148.   DoubleVec    apply2(CmathFunTy2);
  149.   friend    DoubleVec    abs(const DComplexVec&);
  150. //friend    DComplexVec    acos(const DComplexVec&);
  151. //friend    DComplexVec    asin(const DComplexVec&);
  152. //friend    DComplexVec    atan(const DComplexVec&);
  153. //friend    DComplexVec    atan2(const DComplexVec&,const DComplexVec&);
  154. //friend    DComplexVec    ceil(const DComplexVec&);
  155.   friend    DComplexVec    cos(const DComplexVec&);
  156.   friend    DComplexVec    cosh(const DComplexVec&);
  157.   friend    DComplexVec    cumsum(const DComplexVec&);
  158.   friend    DComplexVec    delta(const DComplexVec&);
  159.   friend    DComplex    dot(const DComplexVec&,const DComplexVec&);
  160.   friend    DComplexVec    exp(const DComplexVec&); 
  161. //friend    DComplexVec    floor(const DComplexVec&);
  162.   friend    DComplexVec    log(const DComplexVec&);
  163. //friend    int        max(const DComplexVec&);
  164. //friend    int        min(const DComplexVec&);
  165.   friend    DComplex    mean(const DComplexVec&);
  166.   friend    DComplex    prod(const DComplexVec&);
  167.   friend    DComplexVec    pow(const DComplexVec&,const DComplexVec&);
  168.   friend    DComplexVec    reverse(const DComplexVec&);
  169. //friend    DComplexVec    rint(const DComplexVec&);
  170.   friend    DComplexVec    sin(const DComplexVec&);
  171.   friend    DComplexVec    sinh(const DComplexVec&);
  172.   friend    DComplexVec    sqrt(const DComplexVec&);
  173.   friend    DComplex    sum(const DComplexVec&);
  174. //friend    DComplexVec    tan(const DComplexVec&);
  175. //friend    DComplexVec    tanh(const DComplexVec&);
  176.   friend    double        variance(const DComplexVec&);
  177.  
  178. // Complex specific functions:
  179.   friend    DoubleVec    arg(const DComplexVec& V);
  180.   friend    DComplexVec    conj(const DComplexVec& V);
  181.   friend    DoubleVec    imag(const DComplexVec& V);
  182.   friend    DoubleVec    norm(const DComplexVec& V);
  183.   friend    DoubleVec    real(const DComplexVec& V);
  184.  
  185. #endif
  186.   
  187. };
  188.  
  189. // Other (related) declarations:
  190. DComplexVec        rootsOfOne(int N, unsigned nterms);
  191. Inline DComplexVec    rootsOfOne(int N) {return rootsOfOne(N,N);}
  192. DComplexVec        expandConjugateEven(const DComplexVec&);
  193. DComplexVec        expandConjugateOdd(const DComplexVec&);
  194. ostream&        operator<<(ostream&, const DComplexVec&);
  195. istream&        operator>>(istream&, DComplexVec&);
  196.  
  197. /******************* I N L I N E S **************************/
  198.  
  199. Inline void        DComplexVec::setFormatting(int i){numberPerLine = i;}
  200.  
  201. Inline void        DComplexVec::boundsCheck(int i){
  202.   if(i<0 || i>npts) boundsErr(i);
  203. }
  204. Inline DComplex&    DComplexVec::operator[](int i){
  205.   boundsCheck(i); return begin[i*step];
  206. }
  207. Inline DComplex&    DComplexVec::operator()(int i) {
  208. #if BOUNDS_CHECK    
  209.   boundsCheck(i);
  210. #endif
  211.   return begin[i*step];
  212. }
  213.  
  214. Inline DComplexVec    operator+(const DComplexVec& a)            {return a;}
  215. Inline DComplexVec    operator*(DComplex a, const DComplexVec& b)    {return b*a;}
  216. Inline DComplexVec    operator+(DComplex a, const DComplexVec& b)    {return b+a;}
  217.  
  218. #ifndef NO_VECTOR_MATHFUN
  219. Inline DComplexVec cos(const DComplexVec& V)    { return V.apply(::cos); }
  220. Inline DComplexVec cosh(const DComplexVec& V)    { return V.apply(::cosh); }
  221. Inline DComplexVec exp(const DComplexVec& V)    { return V.apply(::exp); }
  222. Inline DComplexVec log(const DComplexVec& V)    { return V.apply(::log); }
  223. Inline DComplexVec sin(const DComplexVec& V)    { return V.apply(::sin); }
  224. Inline DComplexVec sinh(const DComplexVec& V)    { return V.apply(::sinh); }
  225. Inline DComplexVec sqrt(const DComplexVec& V)    { return V.apply(::sqrt); }
  226. Inline DComplex mean(const DComplexVec& V)    { return sum(V)/double(V.length()); }
  227.  
  228. // Complex specific:
  229. Inline DoubleVec arg(const DComplexVec& V)    { return V.apply2(::arg); }
  230. Inline DComplexVec conj(const DComplexVec& V)    { return V.apply(::conj); }
  231. Inline DoubleVec norm(const DComplexVec& V)    { return V.apply2(::norm); }
  232. #endif
  233.  
  234. #endif
  235.